package com.hymjweb.demo.hymj.out_unit_manage.controller;

import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.out_unit_manage.bean.BOutUnitPojo;
import com.hymjweb.demo.hymj.out_unit_manage.service.BOutUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/pages/out_unit_manage")
public class BOutUnitController extends BaseController {

    @Autowired
    private BOutUnitService bOutUnitService;

    @RequestMapping("/init")
    public String init(HttpServletRequest request) {
        return "pages/out_unit_manage/boutunit";
    }

    @RequestMapping("/query")
    @ResponseBody
    public Object query(@RequestParam(required = false) Map param) throws Exception {
        if(param != null){
            return bOutUnitService.getPageList(param);
        } else{
            throw new Exception("查询参数错误");
        }
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(@RequestBody(required = true) BOutUnitPojo pojo) throws Exception {
        BaseResultMap baseResultMap = new BaseResultMap();
        if (pojo != null) {
            int flag = bOutUnitService.insert(pojo);
            if (flag != 0) {
                baseResultMap.setOk(true);
                baseResultMap.setMessage("添加成功!");
            }
        } else {
            throw new Exception("参数错误");
        }
        return baseResultMap;
    }


    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody(required = true)BOutUnitPojo pojo) throws Exception {
        BaseResultMap baseResultMap = new BaseResultMap();
        if (pojo != null) {
            int flag = bOutUnitService.update(pojo);
            if (flag != 0) {
                baseResultMap.setOk(true);
                baseResultMap.setMessage("修改成功!");
            }
        } else {
            throw new Exception("参数错误");
        }
        return baseResultMap;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody(required = true) String id) throws Exception {
        BaseResultMap baseResultMap = new BaseResultMap();
        if (!DataTypeUtil.isEmptyStr(id)) {
            int flag = bOutUnitService.delete(id);
            if (flag != 0) {
                baseResultMap.setOk(true);
                baseResultMap.setMessage("删除成功!");
            }
        } else {
            throw new Exception("参数错误");
        }
        return baseResultMap;
    }


}
